package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service // dubbo的
public class UserLocationServiceImpl implements UserLocationService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveOrUpdateUserLocation(Double latitude, Double longitude, String addrStr, Long userId) {
        // 1.根据userId查询
        UserLocation userLocation = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(userId)), UserLocation.class);

        if (userLocation == null) { // 2.不存在-新增
            userLocation = new UserLocation();
            userLocation.setUserId(userId); // 用户id
            userLocation.setLocation(new GeoJsonPoint(longitude, latitude)); // 经Y纬X度坐标
            userLocation.setAddress(addrStr); // 位置描述
            userLocation.setCreated(System.currentTimeMillis()); // 创建时间
            userLocation.setUpdated(System.currentTimeMillis()); // 更新时间
            userLocation.setLastUpdated(System.currentTimeMillis()); // 上次更新时间
            mongoTemplate.save(userLocation);

        } else { // 3.存在-更新
            userLocation.setLocation(new GeoJsonPoint(longitude, latitude)); // 经Y纬X度坐标
            userLocation.setAddress(addrStr); // 位置描述
            userLocation.setLastUpdated(userLocation.getUpdated()); // 上次更新时间
            userLocation.setUpdated(System.currentTimeMillis()); // 更新时间
            mongoTemplate.save(userLocation);
        }

    }

    @Override
    public List<Long> searchNearUser(Long userId, Integer distance) { // 单位米
        // 1.查询自己的位置
        UserLocation userLocation = mongoTemplate.findOne(Query.query(Criteria.where("userId").is(userId)), UserLocation.class);

        // 2.指定半径范围
        Distance dis = new Distance(distance / 1000, Metrics.KILOMETERS);// 指定半径单位为公里

        // 3.根据中心点和半径范围，画圆
        Circle circle = new Circle(userLocation.getLocation(), dis);

        // 4.在圆的范围内查找
        Query query = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(query, UserLocation.class);

        // 5.遍历
        List<Long> nearUserIdList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userLocations)) {
            for (UserLocation location : userLocations) {
                nearUserIdList.add(location.getUserId());
            }
        }
        // 6.返回结果
        return nearUserIdList;
    }
}
